//
// Created by 12992 on 24-10-15.
//

#include "ImageWidget.h"

#include <utility>
#include <QPaintEvent>
#include <QPainter>

namespace gui::widgets
{


    ImageWidget::ImageWidget(const QImage& image, QWidget* parent)
            : QWidget(parent), image(QPixmap::fromImage(image))
    {

    }

    ImageWidget::ImageWidget(QPixmap image, QWidget* parent)
            : QWidget(parent), image(std::move(image))
    {

    }

    void ImageWidget::paintEvent(QPaintEvent* event)
    {
        QWidget::paintEvent(event);
        QPainter painter(this);
        auto tempImage = this->image.scaled(this->width(), this->height(),
                                            Qt::KeepAspectRatio, Qt::SmoothTransformation);
        int x = (this->width() - tempImage.width()) / 2;
        int y = (this->height() - tempImage.height()) / 2;
        painter.drawPixmap(x, y, tempImage);
    }

}